package com.android.pagemarket.ui.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;

import com.android.pagemarket.R;
import com.android.pagemarket.business.logic.InitManager;
import com.android.pagemarket.io.protocol.HttpManager;
import com.android.pagemarket.io.protocol.in.HttpAction.ActionListener;
import com.android.pagemarket.io.protocol.in.LoginAction;
import com.android.pagemarket.io.protocol.out.Company;
import com.android.pagemarket.ui.activity.main.RegisterActivity;
import com.android.pagemarket.util.Util;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-11-1
 * @see : 提示登陆弹出框
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class LoginPopWindow extends PopupWindow implements OnClickListener
{
    private Context mContext;
    private EditText com_name;
    private EditText com_num;
    
    public LoginPopWindow(Context context)
    {
        super(LayoutInflater.from(context)
                .inflate(R.layout.popview_login, null),LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mContext = context;
        setBackgroundDrawable(new ColorDrawable(0x99000000));
        setTouchable(true);
        setFocusable(true);
        setOutsideTouchable(false);
        
        initViews();
    }
    
    private void initViews()
    {
        View v = getContentView();
        com_name = (EditText)v.findViewById(R.id.com_name);
        com_num  = (EditText)v.findViewById(R.id.com_num);
        v.findViewById(R.id.btn_login).setOnClickListener(this);
        v.findViewById(R.id.btn_register).setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.btn_register:
            {
                dismiss();
                RegisterActivity.startActivity(mContext);
                break;
            }
            case R.id.btn_login:
            {
                doLogin();
                break;
            }
        }
    }
    
    private void doLogin()
    {
        String name = com_name.getEditableText().toString().trim();
        String num = com_num.getEditableText().toString().trim();
        
        if(name.length() == 0 || !Util.isValidName(name))
        {
            Util.showToast(R.string.tip_name_check);
            return;
        }
        if(num.length() == 0)
        {
            Util.showToast(R.string.tip_num_check);
            return;
        }
        
        LoginAction action = new LoginAction(name, num);
        action.setActionListener(new ActionListener<Company>()
        {
            @Override
            public void onSucceed(Company result)
            {
                if(result.isNoError())
                {
                    InitManager.getInstance().onLogin(result);
                    dismiss();
                }else
                {
                    Util.showToast(result.resultInfo);
                }
            }
            @Override
            public void onError(int resultCode)
            {
                Util.showToast(LoginAction.getErrorMean(resultCode));
            }
        });
        HttpManager.getInstance().submitAction(action);
    }
}
